package org.example.udf;

import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.functions.RuntimeContext;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class RichFunction {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();


//        DataStreamSource<Integer> source = env.fromCollection(Arrays.asList(1, 22, 3));
        DataStreamSource<Integer> source = env.fromElements(1, 2, 3,4);

        source.map(new RichMapFunction<Integer, Integer>() {
            @Override
            public void open(Configuration parameters) throws Exception {
                RuntimeContext context = getRuntimeContext();
                int subtaskI = context.getIndexOfThisSubtask();
                String name = context.getTaskNameWithSubtasks();
                System.out.println("子任务编号=" + subtaskI + ". 启动子任务的名称" + name);
            }

            @Override
            public void close() throws Exception {
                super.close();
            }

            @Override
            public Integer map(Integer value) throws Exception {
                return value + 1;
            }

        });

        source.print();
        env.execute();
    }
}

